package com.zzy.furns.web;

import com.zzy.furns.domain.Cart;
import com.zzy.furns.domain.Member;
import com.zzy.furns.domain.Order;
import com.zzy.furns.domain.OrderItem;
import com.zzy.furns.service.OrderService;
import com.zzy.furns.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

public class OrderServlet extends BasicServlet{

    private OrderService orderService = new OrderServiceImpl();

    /**
     * 生成订单
     */
    protected void saveOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Member member = (Member) req.getSession().getAttribute("member");

        //如果会员没有登陆，则重定向到会员登陆界面
        if (member == null){
            resp.sendRedirect(req.getContextPath() + "/views/member/login.jsp");
            return;
        }

        //会员已经登陆，获取购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null){
            //会员没有购物就直接进入购物车界面执行购物车结账操作
            System.out.println("购物车为空");
            //重定向回购物车原页面
            resp.sendRedirect(req.getHeader("Referer"));
            return;
        }

        String orderId = orderService.saveOrder(cart, member.getId());
        if (orderId == null){
            resp.sendRedirect(req.getHeader("Referer"));
            return;
        }
        req.getSession().setAttribute("orderId",orderId);
        resp.sendRedirect(req.getContextPath() + "/views/order/checkout.jsp");
    }

    /**
     * 显示订单
     */
    protected void showOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");

        //根据订单号查询订单信息
        Order order = orderService.queryOrderByOrderId(orderId);

        //将订单信息保存到Session中
        req.getSession().setAttribute("order",order);

        //请求转发到order.jsp界面 心得：选择请求转发与重定向时，要明确请求的发起页面以及一个请求中经过的流程
        req.getRequestDispatcher("/views/order/order.jsp").forward(req,resp);
    }

    /**
     * 显示订单项列表
     */
    protected void showOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");

        //根据orderId查询订单项列表
        List<OrderItem> orderItemList = orderService.queryOrderItemListByOrderId(orderId);

        //将订单项列表信息保存到Session中
        req.getSession().setAttribute("orderItemList",orderItemList);

        //根据订单项列表获取商品总数
        Integer totalCount = orderService.getTotalCount(orderItemList);
        req.setAttribute("totalCount",totalCount);

        //根据订单项列表获取商品总价
        BigDecimal totalPrice = orderService.getTotalPrice(orderItemList);
        req.setAttribute("totalPrice",totalPrice);

        //请求转发到order_detail.jsp界面
        req.getRequestDispatcher("/views/order/order_detail.jsp").forward(req,resp);
    }
}
